package com.fz.song.bigdatecreate.controller;



import com.fz.song.bigdatecreate.job.SaveUser;
import com.fz.song.bigdatecreate.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private IUserService userService;
    public static ExecutorService threadPool = null;

    @PostConstruct
    public void postConstruct() {
        log.info("线程池初始化...");
        threadPool = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
    }

    @PreDestroy
    public void preDestroy() {
        log.info("正在关闭线程池...");
        threadPool.shutdown();

    }

    @GetMapping("bigdata")
    public String bigdata() {
        for (int i = 0; i < 10; i++) {
            SaveUser saveUser1 = new SaveUser(userService);
            log.info("添加线程到线程池...");
            threadPool.execute(saveUser1);
        }
        return "success";
    }


}
